<?php
class MyCache{
	private static $CacheTime = 5;
	/**
	 * Enter description here...
	 *
	 * @param Array $data cache data
	 * @param string $arrayname cache file name and cache variable
	 * @param Array $cachetime how long this file in cache
	 */
	public static function ReliableCacheDbData($sql,$arrayname,$cachetime=null){
		if($cachetime == null) $cachetime = MyCache::$CacheTime;
		$cache_file = "../cache/$arrayname.php";
		if(!file_exists($cache_file) || time()-@filemtime($cache_file) > $cachetime*60){
			$db = getDb();
			$db->connect();
			$data = $db->fetch($sql);
			$data = serialize($data);
			$data = addslashes($data);
			$phpdata = "<?php\r\n $$arrayname = '$data'; \r\n?>";
			MyCache::WriteFile($cache_file,$phpdata);
		}
		require($cache_file);
		return unserialize(stripslashes($$arrayname));
	}
	/**
	 * Enter description here...
	 *
	 * @param string $func function name which return an array
	 * @param string $arrayname the file name and cache variable
	 * @param int $cachetime  how long the file in cache in minite
	 * @return the result array
	 */
	public static function CacheFunction($func,$arrayname,$cachetime=null,$param=null){
		if($cachetime == null) $cachetime = MyCache::$CacheTime;
		$cache_file = "../cache/$arrayname.php";
		if(!file_exists($cache_file) || time()-@filemtime($cache_file) > $cachetime*60){
			$data = $func($param);
			$data = MyCache::EncodeArray($data);
			$phpdata = "<?php\r\n $$arrayname = $data; \r\n?>";
			MyCache::WriteFile($cache_file,$phpdata);
		}
		require($cache_file);
		return $$arrayname;
	}
	/**
	 * Enter description here...
	 *
	 * @param string $sql the mysql statement
	 * @param string $arrayname the file name and cache variable
	 * @param int $cachetime  how long the file in cache in minite
	 * @return the result array
	 */
	public static function CacheDbData($sql,$arrayname,$cachetime=null){
		if($cachetime == null) $cachetime = MyCache::$CacheTime;
		$cache_file = "../cache/$arrayname.php";
		if(!file_exists($cache_file) || time()-@filemtime($cache_file) > $cachetime*60){
			$db = getDb();
			$db->connect();
			$data = $db->fetch($sql);
			$data = MyCache::EncodeArray($data);
			$phpdata = "<?php\r\n $$arrayname = $data; \r\n?>";
			MyCache::WriteFile($cache_file,$phpdata);
		}
		require($cache_file);
		return $$arrayname;
	}
	public static function WriteFile($filename,$data,$method="rb+",$iflock=1)
	{
		touch($filename);
		$handle=fopen($filename,$method);
		if($iflock)
		{
			flock($handle,LOCK_EX);
		}
		fputs($handle,$data);
		if($method=="rb+") ftruncate($handle,strlen($data));
		fclose($handle);
	}
	public static function EncodeArray($array,$c=1,$t='',$var='')
	{
		$c && $var="array(\r\n";
		$t ="  ";
		if(is_array($array))
		{
		    foreach($array as $key => $value)
		    {
			    $var.="$t'".addslashes($key)."'=>";
			    if(is_array($value))
				{
				    $var.="array(\r\n";
				    $var=MyCache::EncodeArray($value,0,$t,$var);
				    $var.="$t),\r\n";
			    }else{
				    $var.="'".addslashes($value)."',\r\n";
			    }
		     }
	     }
	     if($c)
	     {
		   $var.=")";
	     }
		 return $var;
    }
}
?>